package com.vancouver.knows.ui.activity.my;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.pangu.AMApplication;
import com.pangu.bean.PageInfo;
import com.pangu.service.ActionException;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.PullListItemBrowser;
import com.pangu.ui.refresh.ARefreshViewController;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CommentReportActivity;
import com.vancouver.knows.ui.activity.userpost.UserPostDetailActivity;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.BSharedPrefManager;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.MyAtcommentsInfo;
import com.vancouver.knows.business.service.impl.MyArticleHandle;
import com.vancouver.knows.business.task.mark.MyAtcommentTaskMark;

@SuppressWarnings("rawtypes")
public class MyAtcommentListView extends PullListItemBrowser<AMApplication> {
	private Context mContext;
	private BusinessModule bModule;
	private final String jumpType = "from_my_comment";
	private final String showSoftInput = "show_soft_input";

	public MyAtcommentListView(Context context, ARefreshViewController mController) {
		super(context, mController);
		mContext = context;
		bModule = ((MainApplication) imContext).getBusinessModule();
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		super.receiveResult(taskMark, exception, trackerResult);
		if (exception != null && !exception.getExMessage().equals("")) {
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_LONG).show();
			return;
		}
		if (taskMark instanceof MyAtcommentTaskMark && taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER)
			notifyDataSetChanged();
	}

	@Override
	protected void addListHeader(ListView listView) {
		super.addListHeader(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
		listView.setDividerHeight(1);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		MyAtcommentsInfo info = (MyAtcommentsInfo) parent.getAdapter().getItem(position);
		Intent intent = null;
		if (info.getcPostType().equals("post")) {
			intent = new Intent(getContext(), CityNewsDetailActivity.class);
			intent.putExtra("postId", info.getcPostId());
			intent.putExtra("from_my_comment", jumpType);
			mContext.startActivity(intent);
		} else if (info.getcPostType().equals("business")) {
			intent = new Intent(getContext(), CityDiscountDetailActivity.class);
			intent.putExtra("postId", info.getcPostId());
			intent.putExtra("from_my_comment", jumpType);
			mContext.startActivity(intent);
		} else if (info.getcPostType().equals("userpost")) {
			intent = new Intent(getContext(), UserPostDetailActivity.class);
			intent.putExtra(UserPostDetailActivity.POST_ID_EXTRA, info.getcPostId());
			intent.putExtra(UserPostDetailActivity.JUMP_TYPE_EXTRA, UserPostDetailActivity.JUMP_TYPE_MY_COMMENT);
			mContext.startActivity(intent);
		}

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new MyArticleAdapter();
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
		// 发送登录需要的参数
		MyAtcommentTaskMark myArticleTaskMark = (MyAtcommentTaskMark) taskMark;
		PageInfo pageInfo = myArticleTaskMark.getPageInfo();
		BSharedPrefManager sharedPrefManager = ((MainApplication) imContext).getSharedPrefManager();
		bModule.getServiceWrapper().myArticleFollowAndComments(this, myArticleTaskMark, sharedPrefManager.getUserId(),
				String.valueOf(pageInfo.getNextFrom()), String.valueOf(pageInfo.getSize()), null,
				MyArticleHandle.USER_MENTIONED_COMMENTS);
	}

	public class MyArticleAdapter extends BaseAdapter {

		private LayoutInflater lf;

		public MyArticleAdapter() {
			this.lf = LayoutInflater.from(getContext());
		}

		@Override
		public int getCount() {
			return bModule.getCacheManager().getMyAtcommentCache().getItemInfoCount(mTaskMark);
		}

		@Override
		public Object getItem(int position) {
			return bModule.getCacheManager().getMyAtcommentCache().getItemInfoByTypeIndex(mTaskMark, position);

		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewValueHand hand = null;
			View view = convertView;
			if (position == getCount() - 1) {
				tryQueryNewItems(true, ATaskMark.TYPE_DEFAULT);
			}
			if (convertView == null) {
				view = lf.inflate(R.layout.my_atcomment_view, parent, false);
				hand = new ViewValueHand(view);

				view.setTag(hand);
			} else {
				hand = (ViewValueHand) view.getTag();
			}

			final MyAtcommentsInfo info = (MyAtcommentsInfo) getItem(position);
			hand.commentContent.setText(Html.fromHtml(getString(info.getcAuthor()) + info.getcContent()));
			hand.titleText.setText(info.getcPostTitle());

			hand.replay.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = null;
					if (info.getcPostType().equals("post")) {
						intent = new Intent(getContext(), CityNewsDetailActivity.class);
						intent.putExtra("postId", info.getcPostId());
						intent.putExtra("comment_author", info.getcAuthor());
						intent.putExtra("comment_id", info.getId());
						intent.putExtra("show_soft_input", showSoftInput);
						mContext.startActivity(intent);
					} else if (info.getcPostType().equals("business")) {
						intent = new Intent(getContext(), CityDiscountDetailActivity.class);
						intent.putExtra("postId", info.getcPostId());
						intent.putExtra("comment_author", info.getcAuthor());
						intent.putExtra("comment_id", info.getId());
						intent.putExtra("show_soft_input", showSoftInput);
						mContext.startActivity(intent);
					} else if (info.getcPostType().equals("userpost")) {
						intent = new Intent(getContext(), UserPostDetailActivity.class);
						intent.putExtra(UserPostDetailActivity.POST_ID_EXTRA, info.getcPostId());
						intent.putExtra(UserPostDetailActivity.JUMP_TYPE_EXTRA, UserPostDetailActivity.JUMP_TYPE_MY_COMMENT);
						intent.putExtra("comment_author", info.getcAuthor());
						intent.putExtra("comment_id", info.getId());
						mContext.startActivity(intent);
					}
				}
			});

			hand.report.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (AppUtils.getLoginStatus(getContext())) {
						Intent intent = new Intent(getContext(), CommentReportActivity.class);
						intent.putExtra("target_id", info.getId());
						mContext.startActivity(intent);
					}
				}
			});

			return view;
		}

		public class ViewValueHand {

			@Bind(R.id.comment_content)
			TextView commentContent;
			@Bind(R.id.title_text)
			TextView titleText;

			@Bind(R.id.replay)
			TextView replay;
			@Bind(R.id.report)
			TextView report;

			public ViewValueHand(View view) {
				ButterKnife.bind(this, view);
			}
		}
	}

	private String getString(String author) {
		return "<font color=\"#f3903d\">" + author + "</font>" + " " + mContext.getString(R.string.my_reply) + " "
				+ "<font color=\"#f3903d\">" + mContext.getString(R.string.you) + "</font>" + ":";
	}
}